<template>
  <div class="dashboard-container">
    <aside class="aside-style">
      <el-row>
        <el-col :span="5">
          <el-date-picker
            v-model="datetime"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            :default-time="['00:00:00', '23:59:59']"
            value-format="yyyy-MM-dd"
            clearable
            @change="dateChange"
          />
        </el-col>
        <el-col :span="4">
          <el-input v-model="order_num" clearable placeholder="输入订单号">
            <el-button slot="append" icon="el-icon-search" @click="orderNumChange" />
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-select v-model="department_id" clearable placeholder="请选择部门" @change="departmentChange">
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.department"
              :value="item.id"
            />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select v-model="car_id" clearable placeholder="请选择车辆" @change="carChange">
            <el-option
              v-for="item in carList"
              :key="item.id"
              :label="item.car_name"
              :value="item.id"
            >
              <span style="float: left">{{ item.car_name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.car_num }}</span>
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select
            v-model="user_id"
            filterable
            remote
            clearable
            reserve-keyword
            placeholder="请输入用户名"
            :remote-method="getUserList"
            :loading="loading"
            @change="userChange"
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.user_name"
              :value="item.id"
            >
              <span style="float: left">{{ item.user_name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.department }}</span>
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select
            v-model="driver_id"
            filterable
            remote
            clearable
            reserve-keyword
            placeholder="请输入司机名称"
            :remote-method="getUserList"
            :loading="loading"
            @change="driverChange"
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.user_name"
              :value="item.id"
            >
              <span style="float: left">{{ item.user_name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.department }}</span>
            </el-option>
          </el-select>
        </el-col>
      </el-row>
    </aside>
    <el-table
      :data="list"
      border
      style="width: 100%;margin-top:20px; font-size:12px;"
    >
      <el-table-column
        prop="order_num"
        label="订单"
        width="160"
        align="center"
      />
      <el-table-column
        label="发起"
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.user_info.user_name }}
          <p> <strong>{{ scope.row.department.department }}</strong></p>
        </template>
      </el-table-column>
      <el-table-column
        prop="uname"
        label="乘客姓名"
        align="center"
        width="80"
      />
      <el-table-column
        label="用车"
        width="90"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.car_info.car_name }}
          <p> <strong>{{ scope.row.car_info.car_num }}</strong></p>
        </template>
      </el-table-column>
      <el-table-column
        prop="date_time"
        label="出发时间"
        width="90"
        align="center"
      />
      <el-table-column
        prop="phone"
        label="联系电话"
        width="100"
        align="center"
      />
      <el-table-column
        label="起点"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.start_point.address }}{{ scope.row.start_point.name }}
        </template>
      </el-table-column>
      <el-table-column
        label="终点"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.end_point.address }}{{ scope.row.end_point.name }}
        </template>
      </el-table-column>

      <el-table-column
        prop="mileage"
        label="公里"
        width="60"
      />
      <el-table-column
        prop="number"
        label="人数"
        width="50"
        align="center"
      />
      <el-table-column
        prop="integral"
        width="80"
        label="价格"
        align="center"
      />
      <el-table-column
        prop="receive_time"
        label="接单时间"
        align="center"
        width="90"
      />
      <el-table-column
        prop="create_time"
        label="发起时间"
        align="center"
        width="90"
      />
      <el-table-column
        label="操作"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="checkOrderInfo(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" @pagination="paginationFun" />
    <!-- <upload-process ref="upload-process" @reloadFun="reloadFun" /> -->
    <order-info ref="order-info" />
  </div>
</template>

<script>
import { getCarOrderListApi, getDepartmentListApi, getUserListByNameApi, getCarListApi } from '@/api/main'
import pagination from '@/components/Pagination'
import orderInfo from './components/order-info'
export default {
  components: {
    pagination,
    orderInfo
    // uploadProcess
  },
  data() {
    return {
      userList: [],
      carList: [],
      list: [],
      departmentList: [],
      car_id: '',
      p: 1,
      limit: 10,
      total: 0,
      datetime: '',
      start: '',
      end: '',
      user_id: '',
      driver_id: '',
      loading: false,
      department_id: '',
      order_num: '',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    this.getCarOrdertList()
    this.getDepartmentList()
    this.getCarList()
  },
  methods: {
    checkOrderInfo(row) {
      this.$refs['order-info'].dialogVisible = true
      this.$refs['order-info'].form = row
    },
    driverChange() {
      this.getCarOrdertList()
    },
    orderNumChange() {
      this.getCarOrdertList()
    },
    getUserList(query) {
      this.loading = true
      console.log(query)
      const data = {
        user_name: query
      }
      getUserListByNameApi(data).then(res => {
        if (res.code === 200) {
          this.userList = res.user
          this.loading = false
        }
      })
    },
    paginationFun(val) {
      this.p = val.page
      this.limit = val.limit
      this.getCarOrdertList()
    },
    reloadFun() {
      this.getUserList()
    },
    userChange() {
      this.getCarOrdertList()
    },
    getDepartmentList() {
      getDepartmentListApi().then(res => {
        if (res.code === 200) {
          this.departmentList = res.list
        }
      })
    },
    getCarList() {
      const data = {
        status: 1,
        p: 1,
        limit: 1000000
      }
      getCarListApi(data).then(res => {
        this.carList = res.list
      })
    },
    departmentChange(val) {
      this.department_id = val
      this.getCarOrdertList()
    },
    carChange(val) {
      this.car_id = val
      this.getCarOrdertList()
    },
    dateChange(val) {
      if (val) {
        this.start_time = val[0] + ' 00:00:00'
        this.end_time = val[1] + ' 23:59:59'
      } else {
        this.start_time = ''
        this.end_time = ''
      }
      this.getCarOrdertList()
    },
    getCarOrdertList() {
      const data = {
        status: 2,
        p: this.p,
        limit: this.limit,
        start: this.start_time,
        end: this.end_time,
        user_id: this.user_id,
        car_id: this.car_id,
        driver_id: this.driver_id,
        order_num: this.order_num,
        department_id: this.department_id
      }
      getCarOrderListApi(data).then(res => {
        console.log(res)
        this.list = res.list
        this.total = Number(res.count)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
    &-container {
        margin: 30px;
    }
    &-text {
        font-size: 30px;
        line-height: 46px;
    }
}
.el-input__inner{
  width: 95%;
}
.el-select{
  width: 95%;
}
</style>
